package com.cloud.system.controller;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.pagehelper.PageHelper;
import com.cloud.common.constants.ExcelPoiConstants;
import com.cloud.common.domain.Result;
import com.cloud.controller.BaseController;
import com.cloud.entity.SysLoginLogs;
import com.cloud.enums.BusinessType;
import com.cloud.logs.Logs;
import com.cloud.security.aop.RequestPermissions;
import com.cloud.system.service.ISysLoginLogsService;
import com.cloud.utils.ExcelPoiUtils;
import com.cloud.utils.ServletUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * 登录日志记录
 *
 * @author yzj
 * @since 2022-05-13
 */
@Tag(name = "登录日志")
@RestController
@RequestMapping("/system/loginLogs")
public class SysLoginLogsController extends BaseController {

    @Autowired
    private ISysLoginLogsService logLoginService;

    @Operation(summary = "登录日志记录分页")
    @RequestPermissions("system:loginLogs:list")
    @GetMapping("/list")
    public Result list(SysLoginLogs query) {
        startPage();
        List<SysLoginLogs> list = logLoginService.selectSysLogLoginList(query);
        return getDataTable(list);
    }

    @Operation(summary = "导出登录日志")
    @RequestPermissions("system:loginLogs:export")
    @GetMapping("/export")
    public void export(SysLoginLogs query) {
        PageHelper.startPage(ExcelPoiConstants.startNum, ExcelPoiConstants.maxSize);
        List<SysLoginLogs> list = logLoginService.selectSysLogLoginList(query);
        String titleName = "登录日志列表";
        ExcelPoiUtils.exportExcel(list, titleName, "sys_log_login", SysLoginLogs.class,
                titleName + ".xls", ServletUtils.getResponse());
    }

    @Operation(summary = "清空登录日志")
    @Logs(title = "清空登录日志", businessType = BusinessType.DELETE)
    @RequestPermissions("system:loginLogs:remove")
    @GetMapping("/clean")
    public Result clean() {
        logLoginService.cleanLogLogin();
        return Result.success("操作成功");
    }

    @Operation(summary = "删除登录日志")
    @Logs(title = "删除登录日志", businessType = BusinessType.DELETE)
    @RequestPermissions("system:loginLogs:remove")
    @PostMapping("/remove")
    public Result remove(String loginIds) {
        List<String> list = Arrays.asList(loginIds.split(StringPool.COMMA));
        return toResult(logLoginService.removeByIds(list));
    }

}
